package com.example.pc.updata.DaTi;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.pc.http.API;
import com.example.pc.http.Okhttp;
import com.example.pc.updata.Baes.BaseActivity;
import com.example.pc.updata.R;

import com.example.pc.updata.obj.FanKui;
import com.example.pc.updata.obj.Ts;
import com.example.pc.updata.tools.ActivityTools;
import com.example.pc.updata.tools.MyBaseAdapter;
import com.example.pc.utils.JSONUtils;
import com.example.pc.view.MyListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class AdminTiActivity extends BaseActivity {


    @BindView(R.id.im_left)
    ImageView imLeft;
    @BindView(R.id.my_list)
    MyListView myList;

    List<Ts.DataBean> list_timu = new ArrayList<>();
    MyBaseAdapter<Ts.DataBean> myBaseAdapter;
    @BindView(R.id.btn_back)
    ImageView btnBack;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_ti);
        ButterKnife.bind(this);

        imLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityTools.goActivity(AdminTiActivity.this, TimuActivity.class);
            }
        });
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        myBaseAdapter = new MyBaseAdapter<Ts.DataBean>(AdminTiActivity.this, R.layout.item_ly, list_timu) {
            @Override
            protected void initialData(int position, View converView, ViewGroup arg2) {
                TextView tv = (TextView) converView.findViewById(R.id.tv_text);
                Button btn_up = (Button) converView.findViewById(R.id.btn_up);

                Button btn_del = (Button) converView.findViewById(R.id.btn_del);
                tv.setText("题目" + getList().get(position).getTm());
                final String objectId = "" + getList().get(position).getId();
                final Ts.DataBean tiMu = getList().get(position);
                btn_up.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(AdminTiActivity.this, TimuActivity.class);
                        intent.putExtra("timu", tiMu);
                        startActivity(intent);
                    }
                });
                btn_del.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        API.timudetel(objectId, new Okhttp.Objectcallback() {
                            @Override
                            public void onsuccess(String st) {
                                Toast.makeText(AdminTiActivity.this, "删除成功！", Toast.LENGTH_SHORT).show();
                                findAll();
                            }

                            @Override
                            public void onFalia(int code, String errst) {
                                Toast.makeText(AdminTiActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                            }
                        });

                    }
                });
            }
        };
        myList.setAdapter(myBaseAdapter);


    }

    @Override
    protected void onResume() {
        super.onResume();
        findAll();
    }


    private void findAll() {
        myBaseAdapter.removeAll();
        API.gettimus(new Okhttp.Objectcallback() {
            @Override
            public void onsuccess(String st) {
                Log.e("88", "xx" + st);
                Ts fanKui = JSONUtils.parseJSON(st, Ts.class);
                if (fanKui.getData() != null && fanKui.getData().size() > 0) {
                    myBaseAdapter.setList(fanKui.getData());

                } else {

                }
                myBaseAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFalia(int code, String errst) {
                Log.e("888", "xx" + errst);

            }
        });

    }
}
